<?php
/**
 * Created by PhpStorm.
 * User: zq
 * Date: 2017/2/22
 * Time: 15:14
 */

namespace Admin\Controller;


use Common\Controller\AdminController;

use Common\Model\BrandModel;

class BrandController extends AdminController
{
    public function index(){
        $brandData = m('brand')->select();
//        dump($brandData);die;
        $this->assign('brandData',$brandData);
        $this->display();
    }
    public function add(){
        $id = I('get.bid');
        if(IS_POST){
            $data = I('post.');
            $data['logo'] = $this->upload();
//            dump($data);die;
            if($id){
                $data['bid'] = $id;

            }
            $this->store(new BrandModel(),$data,function(){
                $this->success('操作成功',u('Brand/index'));die;
            });
        }
        //分配旧数据
        if($id){
            $oldData = m('brand')->find($id);
        }else{
            $oldData =[];
        }
//        dump($oldData);die;
        $this->assign('oldData', $oldData);
        $this->display();
    }
//    public function edit(){
//        $id = I('get.bid');
//        if(IS_POST){
//            $data = I('post.');
//            $data['bid'] = $id;
//            $data['logo'] = $this->upload();
//            $brand = new BrandModel();
//            $brand->save($data);
//        }
//        $oldData = new BrandModel();
//        $new = $oldData->where("'bid'=$id")->select();
////        dump($new);die;
//        $this->assign('oldData',$new[0]);
//        $this->display();
//    }
    public function del(){
        $id =I('get.bid');
        $model = new BrandModel();
        if($model->where("bid={$id}")->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * 上传
     */
    public function upload()
    {
//        echo 1;die;
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     ''; // 设置附件上传（子）目录
        // 上传文件
        $info   =   current($upload->upload());
//		echo '<pre>';
//		print_r($info);die;

        return $info['savepath'].$info['savename'];
    }
    /**
     * 异步删除图片
     */
    public function delImg()
    {
        //echo $_POST['path'];die;
        $path = $_POST['path'];
        //删除图片
        //检测文件是否存在，存在的话执行删除
        if(file_exists($path)){
            unlink($path);
        }
        echo 1;exit;
    }

}